package com.financing.controller.agent;
import java.util.Date;
import java.util.List;
import java.util.Random;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;

import com.financing.model.Data;
import com.financing.model.user.User;
import com.financing.service.user.UserService;
import com.financing.util.DateUtil;
import com.financing.util.Json;
import com.financing.util.Str;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;

/**
 *  拜访记录控制器
 * @since 2017-06-06
 * @author 无处不在的二叔
 */
public class AllotController extends Controller {
	private static UserService user = new UserService();

	/**
	 * 用户分配客服
	 */
	public void index() {
		String now =DateUtil.format(new Date(), "HH:mm");
		if(now.equals("09:15") || now.equals("09:16") || now.equals("09:17") || now.equals("09:18") || now.equals("09:19") || now.equals("09:20")
			|| now.equals("13:30") || now.equals("13:31") || now.equals("13:32") || now.equals("13:33") || now.equals("13:34") || now.equals("13:35")
			|| now.equals("17:00") || now.equals("17:01") || now.equals("17:02") || now.equals("17:03") || now.equals("17:04") || now.equals("17:05")){
			String users = getPara("users");
			if(Str.nb(users)){
				Page<User> p = user.list(1,999,Data.flat(getParaMap()));
			    String[] args = Str.split(users,",");
			    Random random = new Random();	
				p.getList().forEach(n->{
					n.setAgentId(Integer.valueOf(args[random.nextInt(args.length)])).update();
				});
				renderJson(Json.ok("分配成功！"));
			}else{
				renderJson(Json.error("客户不可为空！"));
			}
		}else{
			renderJson(Json.error("未到分配时间节点！"));
		}
	}
	

	
	/** 
	 * 修改用户客服
	 */
	@RequiresRoles(logical=Logical.OR,value={"admin"})  
	public void random() {
    	String uid1 = getPara("uid1");
    	String uid2 = getPara("uid2");
    	if(Str.nb(uid2)) {
    		// 开始随机分配给在岗客服*/
    		if(Str.nb(uid1)) {
    			uid1 = " agent_id in("+uid1+")";
    			Db.update("update user set is_agent='0' where id in("+uid1+")");
    		}else {
    			uid1 = " 1=1 ";
    		}
    		List<User> users = User.me.find("select * from user where "+uid1);
    		Random random = new Random();	
    		String[] agents = uid2.split(",");
    		for(User u : users) {
    			if(agents.length>0) {
    				u.setAgentId(Integer.valueOf(agents[random.nextInt(agents.length)])).update();
    			}
    		}
    		Db.update("update user set is_agent='1' where id in("+uid2+")");
			renderJson(Json.ok("客服随机分配成功！"));
    	} else {
			renderJson(Json.error("信息填写不完整！"));
    	}
	}	
	
	/**
	 * 转移所有用户（A--->B）
	 */
	public void migrate() {
		User u1 = User.me.findById(getParaToInt("user1"));
		User u2 = User.me.findById(getParaToInt("user2"));
		if(u1!=null && u2!=null && u1.getIsAgent()==1 && u2.getIsAgent()==1) {
			user.migrate(u1,u2);
			renderJson(Json.ok("操作成功！"));
		}else{
			renderJson(Json.error("用户状态不正确！"));
		}
	}
	
	/**
	 * 经纪人--->投资人
	 */
	public void toUser() {
		User u = User.me.findById(getParaToInt("user"));
		User agent = User.me.findById(u.getAgentId());
		if(u!=null && agent!=null && u.getIsAgent()==1 && agent.getIsAgent()==1) {
			user.migrate(u,agent);
			u.setIsAgent(0).update();
			renderJson(Json.ok("操作成功！"));
		}else{
			renderJson(Json.error("用户状态不正确！"));
		}
	}
	
	/**
	 * 投资人--->经纪人
	 */
	public void toAgent() {
		User u = User.me.findById(getParaToInt("user"));
		if(u!=null) {
			u.setIsAgent(1).update();
			renderJson(Json.ok("操作成功！"));
		}else{
			renderJson(Json.error("用户不存在！"));
		}
	}
	
	/** 
	 * 拜访信息编辑 
	@RequiresRoles(logical=Logical.OR,value={"admin","agent_leader","agent_manager"})  
	public void index() {
		String agents = getPara("value","");
		if(param.findByName("ALLOT_AGENTS").setValue(agents).update()) {

			User creater = getAttr("user");
			AllotLog al = new AllotLog();
			al.setCreaterId(creater.getId());
			al.setCreaterRealName(creater.getRealName());
			al.setAgentsId(agents);
			String names = "";
			for(String s : agents.split(",")) {
				names += User.me.findById(s).getRealName()+",";
			}
			al.setAgentsRealName(names.length()>0 ? names.substring(0, names.length()-1) : names);
			al.setCreateTime(new Date());
			al.save();
			
		    renderJson(Json.ok("客服设置成功！"));
		} else {
		    renderJson(Json.error("客服设置失败！"));
		}
	}	
	
	public void list() { 
		Map<String,String> params = Data.flat(getParaMap());
		int page = getParaToInt("page",1);
		int size = getParaToInt("size",10);
		Page<Visit> p = allotLog.list(page,size,params);
		renderJson(Json.ok("数据获取成功！", Data.put("page",p)));
	}	
	 */
}
 